package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.pojo.Student;
import com.itheima.edu.info.manager.service.IStudentSrevice;
import com.itheima.edu.info.manager.service.impl.StudentServiceImpl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    private IStudentSrevice service = new StudentServiceImpl();

    public void addStudent() throws ParseException {
        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            System.out.println("请输入学号");
             id = sc.next();

            boolean exist = service.isExist(id);

            if (exist) {
                System.out.println("学号已被占用");
            } else {
                break;
            }
        }


        System.out.println("请输入姓名");
        String name = sc.next();


            System.out.println("请输入您的生日");
            String age = sc.next();
        int bir = service.getBirthday(age);
       String zh = bir+" ";


        Student stu = new Student(id,name,zh);

                boolean result = service.addStudent(stu);
                if (result) {
                    System.out.println("新增成功");
                } else {
                    System.out.println("新增失败");
                }
            }




    public void findAllStudent() {
        ArrayList<Student> list = service.findAllStudent();

        if (list.size() == 0) {
            System.out.println("暂无数据 请添加后查询");
            //打断整个方法的执行
            return;
        }

        System.out.println("学号\t\t\t\t姓名\t\t\t\t年龄");
        //lisi.for
        for (Student stu : list) {
            System.out.println(stu);
        }
    }

    /*
    主方法测试
     */
    public static void main(String[] args) throws ParseException {
        StudentController controller = new StudentController();
        controller.addStudent();

        controller.findAllStudent();
    }
}
